/**
 * Copyright 1999-2014 dangdang.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dydl.model.system;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
//import org.hibernate.annotations.NotFound;
//import org.hibernate.annotations.NotFoundAction;

/**
 * @author lishen
 */
@Entity
@Table(name = "sys_user")
public class SysUser implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6633758355332845606L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private String userName;

	private String password;

	private String email;

	private Boolean status;

	private Integer employeeId;

	@ManyToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.EAGER)
	@JoinTable(name = "sys_user_role", joinColumns = @JoinColumn(name = "user_Id", referencedColumnName = "id", unique = false, nullable = false, insertable = false, updatable = false), inverseJoinColumns = @JoinColumn(name = "role_Id", referencedColumnName = "id", unique = false, nullable = false, insertable = false, updatable = false))
	private List<SysRole> roles;

	public SysUser() {
	}

	public SysUser(Long id, String userName) {
		this.id = id;
		this.userName = userName;
	}

	public SysUser(Long id, String userName, String password, String email, Boolean status, Integer employeeId) {
		super();
		this.id = id;
		this.userName = userName;
		this.password = password;
		this.email = email;
		this.status = status;
		this.employeeId = employeeId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Boolean getStatus() {
		return status;
	}

	public void setStatus(Boolean status) {
		this.status = status;
	}

	public Integer getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(Integer employeeId) {
		this.employeeId = employeeId;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", email=" + email + ", status="
				+ status + ", employeeId=" + employeeId + "]";
	}

	public String[] getAuthorities() {
		List<String> ls_roles = new ArrayList<String>();
		List<SysRole> roles = this.getRoles();
		for (SysRole role : roles) {
			ls_roles.add(role.getName());
		}

		String[] roless = ls_roles.toArray(new String[ls_roles.size()]);
		return roless;
	}

	/*
	 * @Override public Collection<? extends GrantedAuthority> getAuthorities() {
	 * //2 List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
	 * List<SysRole> roles=this.getRoles(); for(SysRole role:roles){ auths.add(new
	 * SimpleGrantedAuthority(role.getName())); } return auths; }
	 */

	public List<SysRole> getRoles() {
		return roles;
	}

	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}

}
